// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// C [author] rhyme_lph
// F [email]  rhymelph@gmail.com
// u [github] https://github.com/rhymelph

import 'dart:io';

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/widgets/bottom_sheet/delete_info_bottom_sheet.dart';
import 'package:url_launcher/url_launcher.dart';

class DashboardDrawerProvider extends BaseProvider {
  bool get showContactUs => ((Platform.isAndroid && Config.settingApp.androidStatus == 1) || (Platform.isIOS && Config.settingApp.iosStatus == 1));

  List<Map<String, dynamic>> get drawerList => [
        {
          'title': S.current.home,
          'asset': A.assetsImagesDrawerHome,
        },
        {
          'isLine': true,
        },
        if (Config.hasPermission(PermissionUtils.invoice) || Config.hasPermission(PermissionUtils.quotation))
          {
            'title': S.current.sales,
            'asset': A.assetsImagesDrawerSell,
            'router': RouterGenProviders.salesPage,
          },
        if (Config.hasPermission(PermissionUtils.bill) || Config.hasPermission(PermissionUtils.purchaseOrder) || Config.hasPermission(PermissionUtils.expense))
          {
            'title': S.current.purchases,
            'asset': A.assetsImagesDrawerIncome,
            'router': RouterGenProviders.purchasesPage,
          },
        if (Config.hasPermission(PermissionUtils.creditDebitNote))
          {
            'title': S.current.credit__debit_note,
            'asset': A.assetsImagesDrawerCredit,
            'router': RouterGenProviders.creditDebitPage,
          },
        if (Config.hasPermission(PermissionUtils.productService))
          {
            'title': S.current.products__services,
            'asset': A.assetsImagesDrawerProduce,
            'router': RouterGenProviders.productsAndServicesPage,
          },
        if (Config.hasPermission(PermissionUtils.businessContact))
          {
            'title': S.current.business_contacts,
            'asset': A.assetsImagesDrawerBusiness,
            'router': RouterGenProviders.mainBusinessContactPage,
          },
        if (Config.hasPermission(PermissionUtils.cashBank))
          {'title': S.current.cash_bank, 'asset': A.assetsImagesDrawerCash, 'router': RouterGenProviders.cashAndBankPage},
        if (Config.hasPermission(PermissionUtils.cashBank))
          {
            'isLine': true,
          },
        // if (Config.hasPermission(PermissionUtils.hrPayroll) ||
        //     (!Config.hasPermission(PermissionUtils.hrPayroll) &&
        //         Config.hasPermission(PermissionUtils.homeDashboard)))
        {'title': S.current.hr_payroll, 'asset': A.assetsImagesDrawerHr, 'router': RouterGenProviders.hrAndPayrollPage},
        // if (Config.hasPermission(PermissionUtils.hrPayroll))
        {
          'isLine': true,
        },
        if (Config.hasPermission(PermissionUtils.salesReport) ||
            Config.hasPermission(PermissionUtils.taxesReports) ||
            Config.hasPermission(PermissionUtils.purchaseReports) ||
            Config.hasPermission(PermissionUtils.hrPayrollReports) ||
            Config.hasPermission(PermissionUtils.inventoryReports) ||
            Config.hasPermission(PermissionUtils.accountantReports))
          {'title': S.current.all_reports, 'asset': A.assetsImagesDrawerReport, 'router': RouterGenProviders.allReportsPage},
        if (Config.hasPermission(PermissionUtils.salesReport) ||
            Config.hasPermission(PermissionUtils.taxesReports) ||
            Config.hasPermission(PermissionUtils.purchaseReports) ||
            Config.hasPermission(PermissionUtils.hrPayrollReports) ||
            Config.hasPermission(PermissionUtils.inventoryReports) ||
            Config.hasPermission(PermissionUtils.accountantReports))
          {
            'isLine': true,
          },
        {'title': S.current.settings, 'asset': A.assetsImagesDrawerSetting, 'router': RouterGenProviders.settingPage},
        if (showContactUs)
          {
            'isLine': true,
          },
        if (showContactUs) {'title': S.current.contact_us, 'asset': A.assetsImagesDrawerContactUs, 'router': 'contact'},
      ];

  String currentSelectDrawer = 'Home';

  @override
  void initState() {
    super.initState();
  }

  @override
  Future get dataFuture => null;

  void onClick(String title, String router) {
    if (router == null) return;

    if (router == 'contact') {
      launch(Config.contactUsUrl);
      return;
    }
    if (router == RouterGenProviders.hrAndPayrollPage) {
      if (!Config.canAccessAdvanceFeature) {
        obtainContext?.call((context) {
          showReadyForUpgradeBottomSheet(context);
        });
        return;
      }
    }
    Routes.navigateTo(router);
  }

  void onNavUserProfile() {
    Routes.navigateTo(RouterGenProviders.accountSettingsPage);
  }
}
